import wavefront
from pyglet import gl, image
import context
import groups

def create_skybox(transform, texture, size=500):
    transform = groups.pack_transform(transform)
    
    tex_image = image.load(texture)
    print ">>>>> TEXTURE ", texture, " LOADED"
    imageSeq = image.ImageGrid(tex_image, 3, 4)
    print ">>>>> TEXTURE ", texture, " SPLIT"
    
    halfsize = size / 2
    halfsizec = halfsize * 1.01 #halfsize corrected
    
    tex_coords = (0.0, 0.0,
                  1.0, 0.0,
                  1.0, 1.0,
                  0.0, 1.0)
    
    
    # Up
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[9].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, group,
                    ("v3f/static",
    (-halfsizec, halfsize, -halfsizec,
     halfsizec,halfsize,-halfsizec,
     halfsizec, halfsize, halfsizec,
     -halfsizec, halfsize, halfsizec)), ("t2f/static", tex_coords))
    
    # Down
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[1].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, group,
                            ("v3f/static",
    (-halfsizec,-halfsize,halfsizec,
     halfsizec,-halfsize,halfsizec,
     halfsizec,-halfsize,-halfsizec,
     -halfsizec,-halfsize,-halfsizec)),
                            ("t2f/static", tex_coords))
    
    # Left
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[4].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, group,
                            ("v3f/static",
    (-halfsize,-halfsizec,halfsizec,
    -halfsize,-halfsizec,-halfsizec,
    -halfsize,halfsizec,-halfsizec,
    -halfsize,halfsizec,halfsizec)),
                            ("t2f/static", tex_coords))
    
    # Front
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[5].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, group,
                            ("v3f/static",
    (-halfsizec,-halfsizec,-halfsize,
    halfsizec,-halfsizec,-halfsize,
    halfsizec,halfsizec,-halfsize,
    -halfsizec,halfsizec,-halfsize)),
                            ("t2f/static", tex_coords))
    
    # Right
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[6].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, groups.TextureBindGroup(imageSeq[6].get_texture()),
                            ("v3f/static",
    (halfsize,-halfsizec,-halfsizec,
    halfsize,-halfsizec,halfsizec,
    halfsize,halfsizec,halfsizec,
    halfsize,halfsizec,-halfsizec)),
                            ("t2f/static", tex_coords))
    
    # Back
    group = groups.TransformGroup(groups.TextureBindGroup(imageSeq[7].get_texture()), transform)
    context._context._batch.add(4, gl.GL_QUADS, group,
                            ("v3f/static",
    (halfsizec,-halfsizec,halfsize,
    -halfsizec,-halfsizec,halfsize,
    -halfsizec,halfsizec,halfsize,
    halfsizec,halfsizec,halfsize)),
                            ("t2f/static", tex_coords))

def create_picture(transform, largeur, hauteur, nomTexture):
    transform = groups.pack_transform(transform)
    
    texture = wavefront.catalogueTextures.chargerTexture(nomTexture)
    
    group = groups.TransformGroup(groups.TextureBindGroup(texture), transform)
    
    vlist = []
    
    vlist.append(context._context._batch.add(4, gl.GL_QUADS, group,
    ("v3f/static",(-0.5, 0.0, 0.0,
                   0.5, 0.0, 0.0,
                   0.5, 1.0, 0.0,
                   -0.5, 1.0, 0.0)),
    ("t2f/static",(0.0, 0.0,
                   1.0, 0.0,
                   1.0, 1.0,
                   0.0, 1.0))))
    
    vlist.append(context._context._batch.add(20, gl.GL_QUADS, groups.TransformGroup(None, transform),
    ("v3f/static",
    (
    # Affichage du verso
    -0.5, 0.0, -0.02,
    -0.5, 1.0, -0.02,
     0.5, 1.0, -0.02,
     0.5, 0.0, -0.02,
    
    # Cote droit
    0.5,0.0,0.0,
    0.5,0.0,-0.02,
    0.5,1.0,-0.02,
    0.5,1.0,0.0,
    
    # Cote gauche
    -0.5,0.0,0.0,
    -0.5,1.0,0.0,
    -0.5,1.0,-0.02,
    -0.5,0.0,-0.02,
    
    # Cote haut
    -0.5, 1.0, 0.0,
     0.5, 1.0, 0.0,
     0.5, 1.0,-0.02,
    -0.5, 1.0,-0.02,
    
    # Cote bas
    -0.5, 0.0, 0.0, 
    -0.5, 0.0, -0.02,
     0.5, 0.0, -0.02,
     0.5, 0.0, 0.0)),
    ("c3f/static",
    (1.0, 0.0, 0.0) * 4 + (1.0, 1.0, 1.0) * 16)))

    return vlist


def create_ground(transform, taille, textureName):  
    transform = groups.pack_transform(transform)
    
    texture = wavefront.catalogueTextures.chargerTexture(textureName)
    
    group = groups.TransformGroup(groups.TextureBindGroup(texture), transform)
    
    taille *= 3.
    
    context._context._batch.add(4, gl.GL_QUADS, group,
    ("v3f/static",(0.0, 0.0, taille,
                   taille, 0.0, taille,
                   taille, 0.0, 0.0,
                   0.0, 0.0, 0.0)),
    ("t2f/static",(0.0, 0.0,
                   taille, 0.0,
                   taille, taille,
                   0.0, taille)))
        

